package com.dailyblue.java.design.单例模式;

import lombok.SneakyThrows;

// 双重校验懒汉模式
public class DemoC {
    private static volatile DemoC instance;

    private DemoC() {
    }

    @SneakyThrows
    public static DemoC getInstance() {
        if (instance == null) {
            synchronized (Object.class) {
                System.out.println(Thread.currentThread().getName() + "开始产生对象");
                Thread.sleep(2000);
                if (instance == null) {
                    instance = new DemoC();
                }
                System.out.println(Thread.currentThread().getName() + "产生对象完毕");
            }
        }
        /*synchronized (Object.class) {
            System.out.println(Thread.currentThread().getName() + "开始产生对象");
            Thread.sleep(2000);
            if (instance == null) {
                instance = new DemoC();
            }
            System.out.println(Thread.currentThread().getName() + "产生对象完毕");
        }*/
        return instance;
    }
}
